﻿// -----------------------------------------------------------------------
// Copyright (c) David Kean. All rights reserved.
// -----------------------------------------------------------------------
extern alias pcl;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using pcl::System.Security.Cryptography;
using pcl::System.Security.Cryptography.Adaptation;

namespace Portable.Security.Cryptography
{
    [TestClass]
    public class SymmetricAlgorithmTests
    {
        [TestMethod]
        public void CreateEncryptor1_CallsUnderlyingAlgorithmCreateEncryptor1()
        {
            int callCount = 0;
            var underlyingAlgorithm = ISymmetricAlgorithmFactory.OverrideCreateEncryptor((key, iv) => { callCount++; return null; });

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            algorithm.CreateEncryptor();

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void CreateEncryptor2_CallsUnderlyingAlgorithmCreateEncryptor2()
        {
            int callCount = 0;
            var underlyingAlgorithm = ISymmetricAlgorithmFactory.OverrideCreateEncryptor((key, iv) => { callCount++; return null; });

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            algorithm.CreateEncryptor(new byte[0], new byte[0]);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void CreateDecryptor1_CallsUnderlyingAlgorithmCreateDecryptor1()
        {
            int callCount = 0;
            var underlyingAlgorithm = ISymmetricAlgorithmFactory.OverrideCreateDecryptor((key, iv) => { callCount++; return null; });

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            algorithm.CreateDecryptor();

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void CreateDecryptor2_CallsUnderlyingAlgorithmCreateDecryptor2()
        {
            int callCount = 0;
            var underlyingAlgorithm = ISymmetricAlgorithmFactory.OverrideCreateDecryptor((key, iv) => { callCount++; return null; });

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            algorithm.CreateDecryptor(new byte[0], new byte[0]);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Dispose_CallsUnderlyingAlgorithmDispose()
        {
            int callCount = 0;
            var underlyingAlgorithm = IDisposableFactory.OverrideDispose<ISymmetricAlgorithm>(() => callCount++);

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            algorithm.Dispose();

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Clear_CallsUnderlyingAlgorithmDispose()
        {
            int callCount = 0;
            var underlyingAlgorithm = IDisposableFactory.OverrideDispose<ISymmetricAlgorithm>(() => callCount++);

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            algorithm.Clear();

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void KeySet_CallsUnderlyingAlgorithmKeySet()
        {
            int callCount = 0;
            var underlyingAlgorithm = ISymmetricAlgorithmFactory.OverrideKey(value => { callCount++; });

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            algorithm.Key = new byte[0];

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void IVSet_CallsUnderlyingAlgorithmIVSet()
        {
            int callCount = 0;
            var underlyingAlgorithm = ISymmetricAlgorithmFactory.OverrideIV(value => { callCount++; });

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            algorithm.IV = new byte[0];

            Assert.AreEqual(1, callCount);
        }


        [TestMethod]
        public void KeyGet_CallsUnderlyingAlgorithmKeyGet()
        {
            int callCount = 0;
            var underlyingAlgorithm = ISymmetricAlgorithmFactory.OverrideKey(() => { callCount++; return null; });

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            var ignored = algorithm.Key;

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void IVGet_CallsUnderlyingAlgorithmIVGet()
        {
            int callCount = 0;
            var underlyingAlgorithm = ISymmetricAlgorithmFactory.OverrideIV(() => { callCount++; return null; });

            var algorithm = new SymmetricAlgorithm(underlyingAlgorithm);
            var ignored = algorithm.IV;

            Assert.AreEqual(1, callCount);
        }
    }
}
